<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Resources\WorkerFamiles;
use App\Http\Resources\WorkingFamiles as ResourcesWorkingFamiles;
use App\Models\Order;
use App\Models\WorkingFamily;
use Illuminate\Http\Request;
use App\Models\Worker;

class WorkingFamiles extends Controller
{
    public function GetWorkingFamily(Request $request)
    {
        // 去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $operation_id = $tokenInfo['worker_id'];
        $workers = Worker::where('id', $operation_id)->first();
        if ($workers->user_status == 0) {
            return LMReturn::Error('已离职员工无法查看', '403');
        }
        // 验证完毕，开始查询
        $workings = WorkingFamily::where('is_able', 1)->get();
        $familes = ResourcesWorkingFamiles::collection($workings);
        return json_encode($familes, true);
    }

    public function InsertWorkingFamily(Request $request)
    {
        // 去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $operation_id = $tokenInfo['worker_id'];
        $workers = Worker::where('id', $operation_id)->first();
        if ($workers->user_status == 0) {
            return LMReturn::Error('已离职员工无法操作', '403');
        }
        // 验证完毕
        $stas = 1;
        $all = $request->all();
        if (empty($all['account'])) {
            return LMReturn::Error('未传入开户行账号');
        } else {
            $account = $all['account'];
            $is_have = WorkingFamily::where('is_able', 1)->where('account', $account)->first();
            if (isset($is_have)) {
                return LMReturn::Error('添加失败，该账户已存在');
            }
        }
        if (empty($all['account_name'])) {
            $stas = 0;
            $accountName = '';
        } else {
            $accountName = $all['account_name'];
        }
        if (empty($all['open_bank'])) {
            $stas = 0;
            $openBank = '';
        } else {
            $openBank = $all['open_bank'];
        }
        if (empty($all['tel'])) {
            $stas = 0;
            $tel = '';
        } else {
            $tel = $all['tel'];
        }
        if (empty($all['ein'])) {
            $stas = 0;
            $ein = '';
        } else {
            $ein = $all['ein'];
        }
        if (empty($all['company_adder'])) {
            $stas = 0;
            $companyAddre = '';
        } else {
            $companyAddre = $all['company_adder'];
        }
        $workers = WorkingFamily::where('is_default', 1)->first();
        if (isset($workers)) {
            $is_default = 2;
        } else {
            $is_default = 1;
        }
        $working = new WorkingFamily();
        $working->account_name = $accountName;
        $working->account = $account;
        $working->open_bank = $openBank;
        $working->tel = $tel;
        $working->ein = $ein;
        $working->company_address = $companyAddre;
        $working->is_default = $is_default;
        $working->is_able = 1;
        $working->save();
        if ($stas == 0) {
            return LMReturn::Success('[Worring]:信息录入成功，但系统检测有遗漏信息，请尽量完善信息');
        } else {
            return LMReturn::Success('[Success]:信息录入成功');
        }
    }

    public function DeleWorkingFamily(Request $request)
    {
        // 去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $operation_id = $tokenInfo['worker_id'];
        $workers = Worker::where('id', $operation_id)->first();
        if ($workers->user_status == 0) {
            return LMReturn::Error('已离职员工无法操作', '403');
        }
        // 验证完毕
        $all = $request->all();
        if (empty($all['family_id'])) {
            return LMReturn::Error('未传入账户id');
        }
        $family_id = $all['family_id'];
        $workings = WorkingFamily::where('id', $family_id)->where('is_able', 1)->first();
        if (empty($workings)) {
            return LMReturn::Error('未找到该账户，或该账户已被删除');
        } else {
            $worksList = Order::where('families_id', $family_id)->get();
            if (count($worksList) > 0) {
                return LMReturn::Error('无法删除，此账户已产生订单');
            }
            $workings->is_able = 2;
            $workings->save();
            $is_defual = $workings->is_default;
            if ($is_defual == 1) {
                $workings->is_default = 2;
                $workings->save();
                $new_defual = WorkingFamily::all()->first();
                $new_defual->is_default = 1;
                $new_defual->save();
                return LMReturn::Success('操作成功，您删除的是默认账户，已将其他账户设为默认账户，请确认默认账户');
            } else {
                return LMReturn::Success('操作成功');
            }
        }
    }

    public function UpWorkingFamily(Request $request)
    {
        // 去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $operation_id = $tokenInfo['worker_id'];
        $workers = Worker::where('id', $operation_id)->first();
        if ($workers->user_status == 0) {
            return LMReturn::Error('已离职员工无法操作', '403');
        }
        // 验证完毕
        $all = $request->all();
        if (empty($all['family_id'])) {
            return LMReturn::Error('未传入账户id');
        }
        $family_id = $all['family_id'];
        $workings = WorkingFamily::where('id', $family_id)->where('is_able', 1)->first();
        if (empty($workings)) {
            return LMReturn::Error('未找到该账户，或该账户已被删除');
        } else {
            if (isset($all['tel'])) {
                $workings->tel = $all['tel'];
            }
            if (isset($all['company_address'])) {
                $workings->company_address = $all['company_address'];
            }
            if (isset($all['is_default'])) {
                if ($all['is_default'] == 1) {
                    if ($workings->is_default != 1) {
                        $defau_workings = WorkingFamily::where('is_default', 1)->first();
                        $defau_workings->is_default = 2;
                        $defau_workings->save();
                        $workings->is_default = 1;
                    }
                } else {
                    return LMReturn::Error('不可以将默认删掉');
                }
            }
            $workings->save();
        }
        return LMReturn::Success();
    }
}
